Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
MUI Select | Change how the selected value display in Input

I have an array of objects and I want to bind multiple properties of object in MenuItem but I want to display only a single property to be displayed in Select

enter image description here

In Above image it is showing 10-xyz in select display, it should only show 10.

const [age, setAge] = React.useState("");
  const [options, setOptions] = React.useState([
    {
      name: "xyz",
      age: "10"
    },
    { name: "xyz", age: "20" }
  ]);
  const handleChange = (event) => {
    setAge(event.target.value);
  };

  return (
    <Select
      labelId="demo-simple-select-label"
      id="demo-simple-select"
      value={age}
      label="Age"
      onChange={handleChange}
    >
      {options.map((ele) => {
        return(<MenuItem value={ele.age}>
          {`${ele.age}-${ele.name}`} 
        </MenuItem>)
      })}
    </Select>
  );
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use renderValue prop to change how the Select should display the value in the Input component:

<Select renderValue={(p) => p}

There are 2 more problems in your code:

  1. You should pass a key in your MenuItem to differentiate between each of them in the list. See this for more explanation.

  2. You should use TextField instead of Select if you want to display a label for your Select as explained in my other answer.

I've fixed both issues in the demo below.

For reference, see the Select API here.

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Relatório

0

You're concatenating both keys in the return:

Switch from:

{`${ele.age}-${ele.name}`}

To this:

{`${ele.age}`} 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda